﻿package extend.php
{
    import com.gengine.core.*;
    import com.gengine.debug.*;
    import com.gengine.manager.*;
    import flash.events.*;
    import flash.net.*;
    import flash.utils.*;
    import mortal.common.global.*;

    public class PHPSender extends Object
    {
        private static const HY_SO:String = "hy_so";
        private static var _hasSend:Boolean = true;
        private static var _userNameObject:Object;
        private static var _allUser:Object;
        private static var _infoMap:Object = {};
        public static var isCreateRole:Boolean = false;
        private static var _myShareObject:SharedObject;
        private static var _nextTime:Number = 0;
        private static const Max_Time:int = 500;

        public function PHPSender()
        {
            return;
        }// end function

        private static function getShareObject() : void
        {
            try
            {
                if (_myShareObject == null)
                {
                    _myShareObject = CacheManager.instance.getLocal(HY_SO);
                    _myShareObject.addEventListener(NetStatusEvent.NET_STATUS, onNetStatusEvent);
                }
            }
            catch (e:Error)
            {
            }
            return;
        }// end function

        private static function onNetStatusEvent(event:NetStatusEvent) : void
        {
            return;
        }// end function

        public static function setToPHPMapFrame(param1:int, param2:String, param3:int = 1) : void
        {
            var _loc_8:String = null;
            var _loc_4:* = SenderType.MapFrame;
            if (!param1)
            {
                return;
            }
            var _loc_5:* = MapFrameUtil.getMapFrame(param1);
            var _loc_6:String = "||";
            var _loc_7:* = param1 + _loc_6 + _loc_5 + _loc_6 + param2 + _loc_6 + param3;
            try
            {
                if (ParamsConst.instance.dothingUrl != null && ParamsConst.instance.dothingUrl != "")
                {
                    _loc_8 = ParamsConst.instance.dothingUrl + "&do=" + _loc_4;
                    _loc_8 = _loc_8 + ("&type=" + _loc_4 + "&username=" + encodeURI(ParamsConst.instance.username) + "&phpID=" + ParamsConst.instance.phpID);
                    _loc_8 = _loc_8 + ("&info=" + encodeURI(_loc_7));
                    addQueue(new URLRequest(_loc_8));
                }
                Log.debug(_loc_7);
            }
            catch (e:Error)
            {
            }
            return;
        }// end function

        public static function sendToURLByPHP(param1:int, param2:String = "", param3:Boolean = false) : void
        {
            var str:String;
            var typeInfo:String;
            var type:* = param1;
            var info:* = param2;
            var isForce:* = param3;
            if (!isForce)
            {
                try
                {
                    if (isSend(ParamsConst.instance.username, type) == false)
                    {
                        return;
                    }
                }
                catch (e:Error)
                {
                }
                try
                {
                }
                if (ParamsConst.instance.dothingUrl != null && ParamsConst.instance.dothingUrl != "")
                {
                    str = ParamsConst.instance.dothingUrl + "&do=" + type;
                    str = str + ("&type=" + type + "&username=" + encodeURI(ParamsConst.instance.username) + "&phpID=" + ParamsConst.instance.phpID);
                    if (type >= SenderType.FlashVersion)
                    {
                        str = str + ("&info=" + encodeURI(info));
                    }
                    else if (info != "")
                    {
                        str = str + ("&info=" + encodeURI(type + "->" + info));
                    }
                    else
                    {
                        typeInfo = SenderType.getInfoByType(type);
                        str = str + ("&info=" + encodeURI(type + "->" + ParamsConst.instance.username + "->" + typeInfo));
                    }
                    addQueue(new URLRequest(str));
                }
            }
            catch (e:Error)
            {
                Log.system(e.message);
            }
            return;
        }// end function

        private static function addQueue(param1:URLRequest) : void
        {
            var _loc_2:* = getTimer();
            var _loc_3:* = -_nextTime;
            if (_loc_2 > _nextTime)
            {
                sendToURL(param1);
                _nextTime = _loc_2 + Max_Time;
            }
            else
            {
                setTimeout(sendToURL, _nextTime - _loc_2, param1);
                _nextTime = _nextTime + Max_Time;
            }
            return;
        }// end function

        public static function isSend(param1:String, param2:int) : Boolean
        {
            try
            {
                getShareObject();
                if (_allUser == null)
                {
                    _allUser = _myShareObject.data["userNameObject"];
                    if (_allUser == null)
                    {
                        _allUser = {};
                    }
                }
                if (_userNameObject == null)
                {
                    _userNameObject = _allUser[ParamsConst.instance.loginIP + "_" + param1];
                    if (_userNameObject == null)
                    {
                        _userNameObject = {time:ParamsConst.instance.time};
                        _allUser[ParamsConst.instance.loginIP + "_" + param1] = _userNameObject;
                    }
                }
                if (_userNameObject[param2])
                {
                    return false;
                }
                else
                {
                    _userNameObject[param2] = true;
                }
                _myShareObject.data.userNameObject = _allUser;
                _myShareObject.flush(10000);
                return true;
            }
            catch (e:Error)
            {
            }
            return false;
        }// end function

        public static function sendGift(param1:int) : void
        {
            if (ParamsConst.instance.loginGiftUrl != null && ParamsConst.instance.loginGiftUrl != "")
            {
                sendToURL(new URLRequest(ParamsConst.instance.loginGiftUrl + "&playerid=" + param1));
            }
            return;
        }// end function

        public static function clearUserName(param1:String) : void
        {
            var userObject:Object;
            var date:Date;
            var nowDate:Date;
            var userName:* = param1;
            try
            {
                getShareObject();
                if (_allUser == null)
                {
                    _allUser = _myShareObject.data["userNameObject"];
                }
                if (_allUser)
                {
                    userObject = _allUser[ParamsConst.instance.loginIP + "_" + userName];
                    if (userObject)
                    {
                        date = userObject.time as Date;
                        nowDate = ParamsConst.instance.time as Date;
                        if (date.date != nowDate.date)
                        {
                            delete _allUser[ParamsConst.instance.loginIP + "_" + userName];
                            _myShareObject.data.userNameObject = _allUser;
                            _myShareObject.flush(10000);
                        }
                    }
                }
            }
            catch (e:Error)
            {
                return;
            }
            return;
        }// end function

        public static function renrenAttention() : void
        {
            if (ParamsConst.instance.attentionUrl != null && ParamsConst.instance.attentionUrl != "")
            {
                sendToURL(new URLRequest(ParamsConst.instance.attentionUrl));
            }
            return;
        }// end function

    }
}
